using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SBPweb.Themes.Windows.Skins;
using SBPweb.Themes.Windows;

namespace SBPweb.Controls.Windows.Bar
{
	/// <summary>
	/// Class of ToolStripRenderer
	/// </summary>
	[ToolboxBitmap(typeof(System.Windows.Forms.ToolStripProfessionalRenderer))]
	public partial class ToolStripRenderer : System.Windows.Forms.ToolStripProfessionalRenderer, ISkinnable
	{
		#region Private members

		private bool mUseDefaultSkin;
		private Skin mSkin;
		private Border mBorder;
		private Margin mBorderThin;
		private Margin mContentMargin;
		private Margin sizingMargin;
		private Colors mColor;

		#endregion

		#region Constructor logic

		/// <summary>
		/// Constructor of ToolStripRenderer
		/// </summary>
		public ToolStripRenderer()
		{
			SetStyle();
			InitializeComponent();
			InitializeMargins();
			InitializeColors();

			RegisterEventHandlers();
		}

		/// <summary>
		/// Constructor of ToolStripRenderer
		/// </summary>
		/// <param name="skin">skin</param>
		public ToolStripRenderer(Skin skin)
		{
			mSkin = skin;
			SetStyle();
			InitializeComponent();
			InitializeMargins();
			InitializeColors();

			RegisterEventHandlers();
		}

		private void SetStyle()
		{
		}

		private void RegisterEventHandlers()
		{
			DefaultLookAndFeel.OnSkinChanged += new EventHandler(DefaultLookAndFeel_OnSkinChanged);
		}

		/// <summary>
		/// Initialize
		/// </summary>
		/// <param name="toolStrip">toolstrip</param>
		protected override void Initialize(ToolStrip toolStrip)
		{
			base.Initialize(toolStrip);
		}

		private void InitializeColors()
		{
			if (Skin == null)
			{
				mColor = new Colors();
			}
			else
			{
				mColor = new Colors(Skin.SelectSingleNode("Elements/PopupMenu/Color"));

				Colors fC = new Colors(Skin.SelectSingleNode("Elements/Form/Color"));
				if (mColor.BackColor.Name.StartsWith("$"))
				{
					mColor.BackColor = fC.BackColor;
				}
				if (mColor.ForeColor.Name.StartsWith("$"))
				{
					mColor.ForeColor = fC.ForeColor;
				}
			}
		}

		private void InitializeMargins()
		{
			if (Skin == null)
			{
				mBorder = new Border();
				mBorderThin = new Margin();
				mContentMargin = new Margin();
				sizingMargin = new Margin();
				mColor = new Colors();
			}
			else
			{
				mBorder = new Border(Skin.SelectSingleNode("Elements/PopupMenu/Border"));
				mBorderThin = new Margin(Skin.SelectSingleNode("Elements/PopupMenu/Border/BorderThin"));
				sizingMargin = new Margin(Skin.SelectSingleNode("Elements/MainMenu/Image/SizingMargins"));
				mContentMargin = new Margin(Skin.SelectSingleNode("Elements/MainMenu/ContentMargins"));
			}
		}

		#endregion

		#region Events

		void DefaultLookAndFeel_OnSkinChanged(object sender, EventArgs e)
		{
			InitializeMargins();
		}

		#endregion

		#region Custom draw
		/// <summary>
		/// Occurs when an arrow on a ToolStripItem is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
		{
			base.OnRenderArrow(e);
		}

		/// <summary>
		/// This method draws a border around the button's image. If the background
		/// to be rendered belongs to the empty cell, a string is drawn. Otherwise,
		/// a border is drawn at the edges of the button.
		/// Occurs when the background for a ToolStripButton is rendered
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
		{
			if (Skin == null)
			{
				base.OnRenderButtonBackground(e);
				return;
			}

			bool checkedItem = (e.Item as ToolStripButton).Checked;
			bool mouseOver = false;
			bool mouseDown = Control.MouseButtons != MouseButtons.None;
			bool verticalLocation = e.ToolStrip.Orientation == Orientation.Vertical;
			Point mousePos = Control.MousePosition;
			Point controlPos = e.ToolStrip.PointToClient(mousePos);
			if (
				(
				 !verticalLocation &&
				 (
				  controlPos.X > e.Item.Bounds.X && controlPos.X < e.Item.Bounds.X + e.Item.Bounds.Width &&
				  controlPos.Y > e.Item.Bounds.Y && controlPos.Y < e.Item.Bounds.Y + e.Item.Bounds.Height
				 )
				)
				||
				(
				  verticalLocation &&
				  (
				   controlPos.X > e.Item.Bounds.X && controlPos.X < e.Item.Bounds.X + e.Item.Bounds.Width &&
				   controlPos.Y > e.Item.Bounds.Y && controlPos.Y < e.Item.Bounds.Y + e.Item.Bounds.Height
				  )
				)
			   )
			{
				mouseOver = true;
			}

			sizingMargin = Skin.CachedProperties.ToolStripLinkSelectedMargin;

			System.Drawing.Image img = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "bars.linkselected.png");

			bool imageVertically = Skin.SelectSingleNode("Elements/LinkSelected/Image").Attributes["Layout"].Value.ToLower() == "vertical";

			int imgCount = 5;
			int imgWidth = img.Width;
			int imgHeight = img.Height / imgCount;
			int imgTop = 0;
			int imgLeft = 0;
			bool enabled = e.Item.Enabled;

			if (!imageVertically)
			{
				imgWidth = img.Width / imgCount;
				imgHeight = img.Height;
			}

			if (enabled && mouseOver && mouseDown && checkedItem)
			{
				if (!imageVertically)
				{
					imgLeft = 4 * imgWidth;
				}
				else
				{
					imgTop = 4 * imgHeight;
				}
			}
			else if (enabled && mouseOver && !mouseDown && checkedItem)
			{
				if (!imageVertically)
				{
					imgLeft = 3 * imgWidth;
				}
				else
				{
					imgTop = 3 * imgHeight;
				}
			}
			else if (enabled && !mouseOver && !mouseDown && checkedItem)
			{
				if (!imageVertically)
				{
					imgLeft = 2 * imgWidth;
				}
				else
				{
					imgTop = 2 * imgHeight;
				}
			}
			else if (enabled && mouseOver && mouseDown && !checkedItem)
			{
				if (!imageVertically)
				{
					imgLeft = 1 * imgWidth;
				}
				else
				{
					imgTop = 1 * imgHeight;
				}
			}
			else if (enabled && mouseOver && !mouseDown && !checkedItem)
			{
				imgLeft = 0;
				imgTop = 0;
			}
			else
			{
				base.OnRenderButtonBackground(e);
				return;
			}

			#region Image parts
			Rectangle iLT = new Rectangle(imgLeft, imgTop, sizingMargin.Left, sizingMargin.Top);
			Rectangle iT = new Rectangle(imgLeft + sizingMargin.Left, imgTop, imgWidth - sizingMargin.Horizontal, sizingMargin.Top);
			Rectangle iRT = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop, sizingMargin.Right, sizingMargin.Top);
			Rectangle iLM = new Rectangle(imgLeft, imgTop + sizingMargin.Top, sizingMargin.Left, (imgHeight - 1) - sizingMargin.Vertical);
			Rectangle iM = new Rectangle(imgLeft + sizingMargin.Left, imgTop + sizingMargin.Top, imgWidth - sizingMargin.Horizontal, (imgHeight - 1) - sizingMargin.Vertical);
			Rectangle iRM = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop + sizingMargin.Top, sizingMargin.Right, (imgHeight - 1) - sizingMargin.Vertical);
			Rectangle iLB = new Rectangle(imgLeft, imgTop + imgHeight - sizingMargin.Bottom, sizingMargin.Left, sizingMargin.Bottom);
			Rectangle iB = new Rectangle(imgLeft + sizingMargin.Left, imgTop + imgHeight - sizingMargin.Bottom, imgWidth - sizingMargin.Horizontal, sizingMargin.Bottom);
			Rectangle iRB = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop + imgHeight - sizingMargin.Bottom, sizingMargin.Right, sizingMargin.Bottom);
			#endregion

			Rectangle drawBarRectangle = new Rectangle(2, 1, e.Item.Width - 2, e.Item.Height - 2);
			if (verticalLocation)
			{
				drawBarRectangle = new Rectangle(2, 2, e.Item.Width - 2, e.Item.Height - 2);
			}

			#region Window parts
			Rectangle tLT = new Rectangle(drawBarRectangle.Left, drawBarRectangle.Top, sizingMargin.Left, sizingMargin.Top);
			Rectangle tT = new Rectangle(drawBarRectangle.Left + sizingMargin.Left, drawBarRectangle.Top, drawBarRectangle.Width - sizingMargin.Horizontal, sizingMargin.Top);
			Rectangle tRT = new Rectangle(drawBarRectangle.Right - sizingMargin.Right, drawBarRectangle.Top, sizingMargin.Right, sizingMargin.Top);
			Rectangle tLM = new Rectangle(drawBarRectangle.Left, drawBarRectangle.Top + sizingMargin.Top, sizingMargin.Left, drawBarRectangle.Height - sizingMargin.Vertical);
			Rectangle tM = new Rectangle(drawBarRectangle.Left + sizingMargin.Left, drawBarRectangle.Top + sizingMargin.Top, drawBarRectangle.Width - sizingMargin.Horizontal, drawBarRectangle.Height - sizingMargin.Vertical);
			Rectangle tRM = new Rectangle(drawBarRectangle.Right - sizingMargin.Right, drawBarRectangle.Top + sizingMargin.Top, sizingMargin.Right, drawBarRectangle.Height - sizingMargin.Vertical);
			Rectangle tLB = new Rectangle(drawBarRectangle.Left, drawBarRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Left, sizingMargin.Bottom);
			Rectangle tB = new Rectangle(drawBarRectangle.Left + sizingMargin.Left, drawBarRectangle.Bottom - sizingMargin.Bottom, drawBarRectangle.Width - sizingMargin.Horizontal, sizingMargin.Bottom);
			Rectangle tRB = new Rectangle(drawBarRectangle.Right - sizingMargin.Right, drawBarRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Right, sizingMargin.Bottom);
			#endregion

			#region Drawing
			e.Graphics.DrawImage(img, tLT, iLT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tT, iT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRT, iRT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLM, iLM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tM, iM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRM, iRM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLB, iLB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tB, iB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRB, iRB, GraphicsUnit.Pixel);
			#endregion
		}

		/// <summary>
		/// Occurs when the background for a ToolStripDropDownButton is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs e)
		{
			base.OnRenderDropDownButtonBackground(e);
		}

		/// <summary>
		/// Occurs when the move handle for a ToolStrip is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderGrip(ToolStripGripRenderEventArgs e)
		{
			if (Skin == null)
			{
				base.OnRenderGrip(e);
				return;
			}
		}

		/// <summary>
		/// Draws the margin between an image and its container.  
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
		{
			if (Skin != null)
			{
				return;
			}
			base.OnRenderImageMargin(e);
		}

		/// <summary>
		/// Occurs when the background for a ToolStripItem is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderItemBackground(ToolStripItemRenderEventArgs e)
		{
			base.OnRenderItemBackground(e);
		}

		/// <summary>
		/// Occurs when the image for a selected ToolStripItem is rendered
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e)
		{
			if (Skin == null)
			{
				base.OnRenderItemCheck(e);
				return;
			}

			sizingMargin = Skin.CachedProperties.ToolStripPopupMenuCheckMargin;

			System.Drawing.Image img = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "bars.popupmenucheck.png");

			int imgCount = 2;
			int imgWidth = img.Width / imgCount;
			int imgHeight = img.Height;
			int imgTop = 0;
			int imgLeft = 0;

			if (Skin.SelectSingleNode("Elements/PopupMenuCheck/Image").Attributes["Layout"].Value.ToLower() == "vertical")
			{
				imgWidth = img.Width;
				imgHeight = img.Height / imgCount;
			}

			#region Image parts
			Rectangle iLT = new Rectangle(imgLeft, imgTop, sizingMargin.Left, sizingMargin.Top);
			Rectangle iT = new Rectangle(imgLeft + sizingMargin.Left, imgTop, imgWidth - sizingMargin.Horizontal, sizingMargin.Top);
			Rectangle iRT = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop, sizingMargin.Right, sizingMargin.Top);
			Rectangle iLM = new Rectangle(imgLeft, imgTop + sizingMargin.Top, sizingMargin.Left, (imgHeight - 1) - sizingMargin.Vertical);
			Rectangle iM = new Rectangle(imgLeft + sizingMargin.Left, imgTop + sizingMargin.Top, imgWidth - sizingMargin.Horizontal, (imgHeight - 1) - sizingMargin.Vertical);
			Rectangle iRM = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop + sizingMargin.Top, sizingMargin.Right, (imgHeight - 1) - sizingMargin.Vertical);
			Rectangle iLB = new Rectangle(imgLeft, imgTop + imgHeight - sizingMargin.Bottom, sizingMargin.Left, sizingMargin.Bottom);
			Rectangle iB = new Rectangle(imgLeft + sizingMargin.Left, imgTop + imgHeight - sizingMargin.Bottom, imgWidth - sizingMargin.Horizontal, sizingMargin.Bottom);
			Rectangle iRB = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop + imgHeight - sizingMargin.Bottom, sizingMargin.Right, sizingMargin.Bottom);
			#endregion

			Rectangle drawRectangle = new Rectangle(new Point (2,0), new System.Drawing.Size(e.Item.Size.Height, e.Item.Size.Height));
			if (e.ToolStrip.Orientation == Orientation.Vertical)
			{
				drawRectangle = new Rectangle(new Point(0, 2), new System.Drawing.Size(e.Item.Size.Width, e.Item.Size.Width));
			}

			#region Window parts
			Rectangle tLT = new Rectangle(drawRectangle.Left, drawRectangle.Top, sizingMargin.Left, sizingMargin.Top);
			Rectangle tT = new Rectangle(drawRectangle.Left + sizingMargin.Left, drawRectangle.Top, drawRectangle.Width - sizingMargin.Horizontal, sizingMargin.Top);
			Rectangle tRT = new Rectangle(drawRectangle.Right - sizingMargin.Right, drawRectangle.Top, sizingMargin.Right, sizingMargin.Top);
			Rectangle tLM = new Rectangle(drawRectangle.Left, drawRectangle.Top + sizingMargin.Top, sizingMargin.Left, drawRectangle.Height - sizingMargin.Vertical);
			Rectangle tM = new Rectangle(drawRectangle.Left + sizingMargin.Left, drawRectangle.Top + sizingMargin.Top, drawRectangle.Width - sizingMargin.Horizontal, drawRectangle.Height - sizingMargin.Vertical);
			Rectangle tRM = new Rectangle(drawRectangle.Right - sizingMargin.Right, drawRectangle.Top + sizingMargin.Top, sizingMargin.Right, drawRectangle.Height - sizingMargin.Vertical);
			Rectangle tLB = new Rectangle(drawRectangle.Left, drawRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Left, sizingMargin.Bottom);
			Rectangle tB = new Rectangle(drawRectangle.Left + sizingMargin.Left, drawRectangle.Bottom - sizingMargin.Bottom, drawRectangle.Width - sizingMargin.Horizontal, sizingMargin.Bottom);
			Rectangle tRB = new Rectangle(drawRectangle.Right - sizingMargin.Right, drawRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Right, sizingMargin.Bottom);
			#endregion

			#region Drawing
			e.Graphics.DrawImage(img, tLT, iLT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tT, iT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRT, iRT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLM, iLM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tM, iM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRM, iRM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLB, iLB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tB, iB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRB, iRB, GraphicsUnit.Pixel);
			#endregion

			System.Drawing.Image imgGlyph = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "bars.popupmenucheck_glyph.png");

			Rectangle i = new Rectangle(0, 0, imgGlyph.Width, imgGlyph.Height);

			Point drawGlypg = new Point(tM.X, tM.Y);

			int sizeMin = imgGlyph.Width > imgGlyph.Height ? imgGlyph.Height : imgGlyph.Width;

			e.Graphics.DrawImageUnscaled(imgGlyph, new Point(drawRectangle.X + (drawRectangle.Width - sizeMin) / 2, drawRectangle.Y + (drawRectangle.Height - sizeMin) / 2));
		}

		/// <summary>
		/// Occurs when the image for a ToolStripItem is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderItemImage(ToolStripItemImageRenderEventArgs e)
		{
			base.OnRenderItemImage(e);
		}

		/// <summary>
		/// Occurs when the text for a ToolStripItem is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
		{
			base.OnRenderItemText(e);
		}

		/// <summary>
		/// Occurs when the background for a ToolStripLabel is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderLabelBackground(ToolStripItemRenderEventArgs e)
		{
			base.OnRenderLabelBackground(e);
		}

		/// <summary>
		/// Occurs when the background for a ToolStripMenuItem is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
		{
			if (Skin != null)
			{
				if (e.Item.Owner is System.Windows.Forms.MenuStrip) //header
				{
					if ((e.Item.Selected || e.Item.Pressed))
					{
						sizingMargin = Skin.CachedProperties.ToolStripLinkSelectedMargin;

						System.Drawing.Image img = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "bars.linkselected.png");

						int imgWidth = img.Width;
						int imgHeight = img.Height / 5;
						int imgTop = 0;
						int imgLeft = 0;

						#region Image parts
						Rectangle iLT = new Rectangle(imgLeft, imgTop, sizingMargin.Left, sizingMargin.Top);
						Rectangle iT = new Rectangle(imgLeft + sizingMargin.Left, imgTop, imgWidth - sizingMargin.Horizontal, sizingMargin.Top);
						Rectangle iRT = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop, sizingMargin.Right, sizingMargin.Top);
						Rectangle iLM = new Rectangle(imgLeft, imgTop + sizingMargin.Top, sizingMargin.Left, (imgHeight - 1) - sizingMargin.Vertical);
						Rectangle iM = new Rectangle(imgLeft + sizingMargin.Left, imgTop + sizingMargin.Top, imgWidth - sizingMargin.Horizontal, (imgHeight - 1) - sizingMargin.Vertical);
						Rectangle iRM = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop + sizingMargin.Top, sizingMargin.Right, (imgHeight - 1) - sizingMargin.Vertical);
						Rectangle iLB = new Rectangle(imgLeft, imgTop + imgHeight - sizingMargin.Bottom, sizingMargin.Left, sizingMargin.Bottom);
						Rectangle iB = new Rectangle(imgLeft + sizingMargin.Left, imgTop + imgHeight - sizingMargin.Bottom, imgWidth - sizingMargin.Horizontal, sizingMargin.Bottom);
						Rectangle iRB = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop + imgHeight - sizingMargin.Bottom, sizingMargin.Right, sizingMargin.Bottom);
						#endregion

						Rectangle drawingRectangle = new Rectangle((int)e.Graphics.VisibleClipBounds.X, (int)e.Graphics.VisibleClipBounds.Y, e.Item.Size.Width, e.Item.Size.Height);
						//	e.Item.ContentRectangle;

						#region Window parts
						Rectangle tLT = new Rectangle(drawingRectangle.Left, drawingRectangle.Top, sizingMargin.Left, sizingMargin.Top);
						Rectangle tT = new Rectangle(drawingRectangle.Left + sizingMargin.Left, drawingRectangle.Top, drawingRectangle.Width - sizingMargin.Horizontal, sizingMargin.Top);
						Rectangle tRT = new Rectangle(drawingRectangle.Right - sizingMargin.Right, drawingRectangle.Top, sizingMargin.Right, sizingMargin.Top);
						Rectangle tLM = new Rectangle(drawingRectangle.Left, drawingRectangle.Top + sizingMargin.Top, sizingMargin.Left, drawingRectangle.Height - sizingMargin.Vertical);
						Rectangle tM = new Rectangle(drawingRectangle.Left + sizingMargin.Left, drawingRectangle.Top + sizingMargin.Top, drawingRectangle.Width - sizingMargin.Horizontal, drawingRectangle.Height - sizingMargin.Vertical);
						Rectangle tRM = new Rectangle(drawingRectangle.Right - sizingMargin.Right, drawingRectangle.Top + sizingMargin.Top, sizingMargin.Right, drawingRectangle.Height - sizingMargin.Vertical);
						Rectangle tLB = new Rectangle(drawingRectangle.Left, drawingRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Left, sizingMargin.Bottom);
						Rectangle tB = new Rectangle(drawingRectangle.Left + sizingMargin.Left, drawingRectangle.Bottom - sizingMargin.Bottom, drawingRectangle.Width - sizingMargin.Horizontal, sizingMargin.Bottom);
						Rectangle tRB = new Rectangle(drawingRectangle.Right - sizingMargin.Right, drawingRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Right, sizingMargin.Bottom);
						#endregion

						#region Drawing
						e.Graphics.DrawImage(img, tLT, iLT, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tT, iT, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tRT, iRT, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tLM, iLM, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tM, iM, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tRM, iRM, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tLB, iLB, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tB, iB, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tRB, iRB, GraphicsUnit.Pixel);
						#endregion
					}
					else // Not selected header
					{
						base.OnRenderMenuItemBackground(e);
					}
				}
				else //menu item
				{
					if ((e.Item.Selected || e.Item.Pressed)) //selected menu item
					{
						sizingMargin = Skin.CachedProperties.ToolStripPopupMenuLinkSelectedMargin;

						System.Drawing.Image img = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "bars.popupmenulinkselected.png");

						bool imageVertically = Skin.SelectSingleNode("Elements/PopupMenuLinkSelected/Image").Attributes["Layout"].Value.ToLower() == "vertical";

						int imgCount = 2;
						int imgWidth = img.Width - 1;
						int imgHeight = img.Height / imgCount;
						int imgTop = 0;
						int imgLeft = 0;

						if (!imageVertically)
						{
							imgWidth = img.Width / imgCount;
							imgHeight = img.Height - 1;
						}

						#region Image parts
						Rectangle iLT = new Rectangle(imgLeft, imgTop, sizingMargin.Left, sizingMargin.Top);
						Rectangle iT = new Rectangle(imgLeft + sizingMargin.Left, imgTop, imgWidth - sizingMargin.Horizontal, sizingMargin.Top);
						Rectangle iRT = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop, sizingMargin.Right, sizingMargin.Top);
						Rectangle iLM = new Rectangle(imgLeft, imgTop + sizingMargin.Top, sizingMargin.Left, (imgHeight - 1) - sizingMargin.Vertical);
						Rectangle iM = new Rectangle(imgLeft + sizingMargin.Left, imgTop + sizingMargin.Top, imgWidth - sizingMargin.Horizontal, (imgHeight - 1) - sizingMargin.Vertical);
						Rectangle iRM = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop + sizingMargin.Top, sizingMargin.Right, (imgHeight - 1) - sizingMargin.Vertical);
						Rectangle iLB = new Rectangle(imgLeft, imgTop + imgHeight - sizingMargin.Bottom, sizingMargin.Left, sizingMargin.Bottom);
						Rectangle iB = new Rectangle(imgLeft + sizingMargin.Left, imgTop + imgHeight - sizingMargin.Bottom, imgWidth - sizingMargin.Horizontal, sizingMargin.Bottom);
						Rectangle iRB = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop + imgHeight - sizingMargin.Bottom, sizingMargin.Right, sizingMargin.Bottom);
						#endregion

						Rectangle drawBarRectangle = new Rectangle((int)e.Graphics.VisibleClipBounds.X + 2, (int)e.Graphics.VisibleClipBounds.Y, (int)e.Graphics.VisibleClipBounds.Width - 4, (int)e.Graphics.VisibleClipBounds.Height);
						if (e.ToolStrip.Orientation == Orientation.Vertical)
						{
							drawBarRectangle = new Rectangle((int)e.Graphics.VisibleClipBounds.X, (int)e.Graphics.VisibleClipBounds.Y + 2, (int)e.Graphics.VisibleClipBounds.Width, (int)e.Graphics.VisibleClipBounds.Height - 4);
						}

						#region Window parts
						Rectangle tLT = new Rectangle(drawBarRectangle.Left, drawBarRectangle.Top, sizingMargin.Left, sizingMargin.Top);
						Rectangle tT = new Rectangle(drawBarRectangle.Left + sizingMargin.Left, drawBarRectangle.Top, drawBarRectangle.Width - sizingMargin.Horizontal, sizingMargin.Top);
						Rectangle tRT = new Rectangle(drawBarRectangle.Right - sizingMargin.Right, drawBarRectangle.Top, sizingMargin.Right, sizingMargin.Top);
						Rectangle tLM = new Rectangle(drawBarRectangle.Left, drawBarRectangle.Top + sizingMargin.Top, sizingMargin.Left, drawBarRectangle.Height - sizingMargin.Vertical);
						Rectangle tM = new Rectangle(drawBarRectangle.Left + sizingMargin.Left, drawBarRectangle.Top + sizingMargin.Top, drawBarRectangle.Width - sizingMargin.Horizontal, drawBarRectangle.Height - sizingMargin.Vertical);
						Rectangle tRM = new Rectangle(drawBarRectangle.Right - sizingMargin.Right, drawBarRectangle.Top + sizingMargin.Top, sizingMargin.Right, drawBarRectangle.Height - sizingMargin.Vertical);
						Rectangle tLB = new Rectangle(drawBarRectangle.Left, drawBarRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Left, sizingMargin.Bottom);
						Rectangle tB = new Rectangle(drawBarRectangle.Left + sizingMargin.Left, drawBarRectangle.Bottom - sizingMargin.Bottom, drawBarRectangle.Width - sizingMargin.Horizontal, sizingMargin.Bottom);
						Rectangle tRB = new Rectangle(drawBarRectangle.Right - sizingMargin.Right, drawBarRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Right, sizingMargin.Bottom);
						#endregion

						#region Drawing
						e.Graphics.DrawImage(img, tLT, iLT, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tT, iT, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tRT, iRT, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tLM, iLM, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tM, iM, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tRM, iRM, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tLB, iLB, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tB, iB, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tRB, iRB, GraphicsUnit.Pixel);
						#endregion
					}
					else //not selected menu item
					{
						e.Item.BackColor = Skin.CachedProperties.PopupMenuBackColor;
						e.Item.ForeColor = Skin.CachedProperties.PopupMenuForeColor;

						base.OnRenderMenuItemBackground(e);

						sizingMargin = Skin.CachedProperties.ToolStripPopupMenuSideStripMargin;

						System.Drawing.Image img = Skin.GetImageFromBitmapResource(Skin.KeyPrefix + "bars.popupmenusidestrip.png");

						int imgWidth = img.Width;
						int imgHeight = img.Height;
						int imgTop = 0;
						int imgLeft = 0;

						#region Image parts
						Rectangle iLT = new Rectangle(imgLeft, imgTop, sizingMargin.Left, sizingMargin.Top);
						Rectangle iT = new Rectangle(imgLeft + sizingMargin.Left, imgTop, imgWidth - sizingMargin.Horizontal, sizingMargin.Top);
						Rectangle iRT = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop, sizingMargin.Right, sizingMargin.Top);
						Rectangle iLM = new Rectangle(imgLeft, imgTop + sizingMargin.Top, sizingMargin.Left, (imgHeight - 1) - sizingMargin.Vertical);
						Rectangle iM = new Rectangle(imgLeft + sizingMargin.Left, imgTop + sizingMargin.Top, imgWidth - sizingMargin.Horizontal, (imgHeight - 1) - sizingMargin.Vertical);
						Rectangle iRM = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop + sizingMargin.Top, sizingMargin.Right, (imgHeight - 1) - sizingMargin.Vertical);
						Rectangle iLB = new Rectangle(imgLeft, imgTop + imgHeight - sizingMargin.Bottom, sizingMargin.Left, sizingMargin.Bottom);
						Rectangle iB = new Rectangle(imgLeft + sizingMargin.Left, imgTop + imgHeight - sizingMargin.Bottom, imgWidth - sizingMargin.Horizontal, sizingMargin.Bottom);
						Rectangle iRB = new Rectangle(imgLeft + imgWidth - sizingMargin.Right, imgTop + imgHeight - sizingMargin.Bottom, sizingMargin.Right, sizingMargin.Bottom);
						#endregion

						Rectangle drawBarRectangle = new Rectangle((int)e.Graphics.VisibleClipBounds.X, (int)e.Graphics.VisibleClipBounds.Y, 25, (int)e.Graphics.VisibleClipBounds.Height);
						if (e.ToolStrip.Orientation == Orientation.Vertical)
						{
							drawBarRectangle = new Rectangle((int)e.Graphics.VisibleClipBounds.X + 2, (int)e.Graphics.VisibleClipBounds.Y + 2, (int)e.Graphics.VisibleClipBounds.Width - 3, (int)e.Graphics.VisibleClipBounds.Height - 1);
						}

						#region Window parts
						Rectangle tLT = new Rectangle(drawBarRectangle.Left, drawBarRectangle.Top, sizingMargin.Left, sizingMargin.Top);
						Rectangle tT = new Rectangle(drawBarRectangle.Left + sizingMargin.Left, drawBarRectangle.Top, drawBarRectangle.Width - sizingMargin.Horizontal, sizingMargin.Top);
						Rectangle tRT = new Rectangle(drawBarRectangle.Right - sizingMargin.Right, drawBarRectangle.Top, sizingMargin.Right, sizingMargin.Top);
						Rectangle tLM = new Rectangle(drawBarRectangle.Left, drawBarRectangle.Top + sizingMargin.Top, sizingMargin.Left, drawBarRectangle.Height - sizingMargin.Vertical);
						Rectangle tM = new Rectangle(drawBarRectangle.Left + sizingMargin.Left, drawBarRectangle.Top + sizingMargin.Top, drawBarRectangle.Width - sizingMargin.Horizontal, drawBarRectangle.Height - sizingMargin.Vertical);
						Rectangle tRM = new Rectangle(drawBarRectangle.Right - sizingMargin.Right, drawBarRectangle.Top + sizingMargin.Top, sizingMargin.Right, drawBarRectangle.Height - sizingMargin.Vertical);
						Rectangle tLB = new Rectangle(drawBarRectangle.Left, drawBarRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Left, sizingMargin.Bottom);
						Rectangle tB = new Rectangle(drawBarRectangle.Left + sizingMargin.Left, drawBarRectangle.Bottom - sizingMargin.Bottom, drawBarRectangle.Width - sizingMargin.Horizontal, sizingMargin.Bottom);
						Rectangle tRB = new Rectangle(drawBarRectangle.Right - sizingMargin.Right, drawBarRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Right, sizingMargin.Bottom);
						#endregion

						#region Drawing
						e.Graphics.DrawImage(img, tLT, iLT, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tT, iT, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tRT, iRT, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tLM, iLM, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tM, iM, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tRM, iRM, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tLB, iLB, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tB, iB, GraphicsUnit.Pixel);
						e.Graphics.DrawImage(img, tRB, iRB, GraphicsUnit.Pixel);
						#endregion
					}
				}
			}
			else
			{
				base.OnRenderMenuItemBackground(e);
			}
		}

		/// <summary>
		/// Occurs when the background for an overflow button is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderOverflowButtonBackground(ToolStripItemRenderEventArgs e)
		{
			if (Skin == null)
			{
				base.OnRenderOverflowButtonBackground(e);
				return;
			}

			bool verticalLocation = e.ToolStrip.Orientation == Orientation.Vertical;
			System.Drawing.Image imgCust = Skin.GetImageFromBitmapResource(string.Format("{0}bars.barcustomize{1}.png", Skin.KeyPrefix, verticalLocation ? "vertical" : string.Empty));
			Margin custSizingMargin = new Margin(Skin.SelectSingleNode(string.Format("Elements/BarCustomize{0}/Image/SizingMargins", verticalLocation ? "Vertical" : string.Empty)));
			bool imageVertically = Skin.SelectSingleNode(string.Format("Elements/BarCustomize{0}/Image", verticalLocation ? "Vertical" : string.Empty)).Attributes["Layout"].Value.ToLower() == "vertical";

			int imgCustCount = 2;
			int imgCustWidth = imgCust.Width;
			int imgCustHeight = (Int32)(imgCust.Height / imgCustCount);
			int imgCustTop = 0;
			int imgCustLeft = 0;
			bool showDefaultCustomization = true;
			if (!imageVertically)
			{
				imgCustWidth = imgCust.Width / imgCustCount;
				imgCustHeight = imgCust.Height;
			}

			Point mousePos = Control.MousePosition;
			Point controlPos = e.ToolStrip.PointToClient(mousePos);
			if (
				(
				 (e.ToolStrip as Bar).OverflowButton.Pressed
				)
				||
				(
				 !verticalLocation &&
				 (
				  controlPos.X > e.ToolStrip.Width - 15 && controlPos.X < e.ToolStrip.Width &&
				  controlPos.Y > 0 && controlPos.Y < e.ToolStrip.Height
				 )
				)
				||
				(
				  verticalLocation &&
				  (
				   controlPos.X > 0 && controlPos.X < e.ToolStrip.Width &&
				   controlPos.Y > e.ToolStrip.Height - 15 && controlPos.Y < e.ToolStrip.Height
				  )
				)
			   )
			{
				showDefaultCustomization = false;
			}

			if (!showDefaultCustomization)
			{
				if (!imageVertically)
				{
					imgCustLeft = imgCustWidth;
				}
				else
				{
					imgCustTop = imgCustHeight;
				}
			}

			#region Image parts
			Rectangle icLT = new Rectangle(imgCustLeft, imgCustTop, custSizingMargin.Left, custSizingMargin.Top);
			Rectangle icT = new Rectangle(imgCustLeft + custSizingMargin.Left, imgCustTop, imgCustWidth - custSizingMargin.Horizontal, custSizingMargin.Top);
			Rectangle icRT = new Rectangle(imgCustLeft + imgCustWidth - custSizingMargin.Right, imgCustTop, custSizingMargin.Right, custSizingMargin.Top);
			Rectangle icLM = new Rectangle(imgCustLeft, imgCustTop + custSizingMargin.Top, custSizingMargin.Left, imgCustHeight - custSizingMargin.Vertical);
			Rectangle icM = new Rectangle(imgCustLeft + custSizingMargin.Left, imgCustTop + custSizingMargin.Top, imgCustWidth - custSizingMargin.Horizontal, imgCustHeight - custSizingMargin.Vertical);
			Rectangle icRM = new Rectangle(imgCustLeft + imgCustWidth - custSizingMargin.Right, imgCustTop + custSizingMargin.Top, custSizingMargin.Right, imgCustHeight - custSizingMargin.Vertical);
			Rectangle icLB = new Rectangle(imgCustLeft, imgCustTop + imgCustHeight - custSizingMargin.Bottom, custSizingMargin.Left, custSizingMargin.Bottom);
			Rectangle icB = new Rectangle(imgCustLeft + custSizingMargin.Left, imgCustTop + imgCustHeight - custSizingMargin.Bottom, imgCustWidth - custSizingMargin.Horizontal, custSizingMargin.Bottom);
			Rectangle icRB = new Rectangle(imgCustLeft + imgCustWidth - custSizingMargin.Right, imgCustTop + imgCustHeight - custSizingMargin.Bottom, custSizingMargin.Right, custSizingMargin.Bottom);
			#endregion

			Rectangle drawCustRectangle = new Rectangle(e.Item.Bounds.Width - 15, e.Item.Bounds.Y, 15, e.Item.Bounds.Height);
			if (verticalLocation)
			{
				drawCustRectangle = new Rectangle(e.Item.Bounds.X, e.Item.Bounds.Height - 15, e.Item.Bounds.Width, 15);
			}

			#region Window parts
			Rectangle tcLT = new Rectangle(drawCustRectangle.Left, drawCustRectangle.Top, custSizingMargin.Left, custSizingMargin.Top);
			Rectangle tcT = new Rectangle(drawCustRectangle.Left + custSizingMargin.Left, drawCustRectangle.Top, drawCustRectangle.Width - custSizingMargin.Horizontal, custSizingMargin.Top);
			Rectangle tcRT = new Rectangle(drawCustRectangle.Right - custSizingMargin.Right, drawCustRectangle.Top, custSizingMargin.Right, custSizingMargin.Top);
			Rectangle tcLM = new Rectangle(drawCustRectangle.Left, drawCustRectangle.Top + custSizingMargin.Top, custSizingMargin.Left, drawCustRectangle.Height - custSizingMargin.Vertical);
			Rectangle tcM = new Rectangle(drawCustRectangle.Left + custSizingMargin.Left, drawCustRectangle.Top + custSizingMargin.Top, drawCustRectangle.Width - custSizingMargin.Horizontal, drawCustRectangle.Height - custSizingMargin.Vertical);
			Rectangle tcRM = new Rectangle(drawCustRectangle.Right - custSizingMargin.Right, drawCustRectangle.Top + custSizingMargin.Top, custSizingMargin.Right, drawCustRectangle.Height - custSizingMargin.Vertical);
			Rectangle tcLB = new Rectangle(drawCustRectangle.Left, drawCustRectangle.Bottom - custSizingMargin.Bottom, custSizingMargin.Left, custSizingMargin.Bottom);
			Rectangle tcB = new Rectangle(drawCustRectangle.Left + custSizingMargin.Left, drawCustRectangle.Bottom - custSizingMargin.Bottom, drawCustRectangle.Width - custSizingMargin.Horizontal, custSizingMargin.Bottom);
			Rectangle tcRB = new Rectangle(drawCustRectangle.Right - custSizingMargin.Right, drawCustRectangle.Bottom - custSizingMargin.Bottom, custSizingMargin.Right, custSizingMargin.Bottom);
			#endregion

			#region Drawing
			e.Graphics.DrawImage(imgCust, tcLT, icLT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgCust, tcT, icT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgCust, tcRT, icRT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgCust, tcLM, icLM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgCust, tcM, icM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgCust, tcRM, icRM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgCust, tcLB, icLB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgCust, tcB, icB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgCust, tcRB, icRB, GraphicsUnit.Pixel);
			#endregion

			#region BarCustomizeArrow
			System.Drawing.Image imgCustArr = Skin.GetImageFromBitmapResource(string.Format("{0}bars.barcustomize{1}_glyph.png", Skin.KeyPrefix, verticalLocation ? "vertical" : string.Empty));

			int imgCustArrCount = 2;
			int imgCustArrWidth = imgCustArr.Width;
			int imgCustArrHeight = (Int32)(imgCustArr.Height / imgCustArrCount);
			int imgCustArrTop = 0;
			int imgCustArrLeft = 0;

			if (!showDefaultCustomization)
			{
				imgCustArrTop = imgCustArrHeight;
			}

			Rectangle i = new Rectangle(imgCustArrLeft, imgCustArrTop, imgCustArrWidth, imgCustArrHeight);

			Point drawCustArrPoint = new Point(e.Item.Bounds.Width - 7 - imgCustArrWidth / 2, (drawCustRectangle.Height - imgCustArrHeight) / 2);
			if (verticalLocation)
			{
				drawCustArrPoint = new Point(drawCustRectangle.Width - 7 - imgCustArrWidth / 2, (e.Item.Bounds.Height - imgCustArrHeight) / 2);
			}

			e.Graphics.DrawImage(imgCustArr, drawCustArrPoint.X, drawCustArrPoint.Y, i, GraphicsUnit.Pixel);
			#endregion
		}

		/// <summary>
		/// Occurs when a ToolStripSeparator is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
		{
			
			if (Skin == null)
			{
				base.OnRenderSeparator(e);
				return;
			}

			bool verticalOrientation = !e.Vertical;

			Image img = verticalOrientation ? Skin.CachedProperties.BarVerticalSeparatorImage : Skin.CachedProperties.BarSeparatorImage;

			RectangleF target = verticalOrientation ? new RectangleF(new PointF(e.Item.Padding.Left, e.Item.Padding.Top), new System.Drawing.Size(e.Item.Size.Width - e.Item.Padding.Horizontal, e.Item.Size.Height))
				: new RectangleF(new PointF(e.Item.Padding.Top, e.Item.Padding.Left), new System.Drawing.Size(e.Item.Size.Width, e.Item.Size.Height - e.Item.Padding.Vertical));
			e.Graphics.DrawImage(img, target);
		}

		/// <summary>
		/// Occurs when the background for a ToolStripSplitButton is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderSplitButtonBackground(ToolStripItemRenderEventArgs e)
		{
			base.OnRenderSplitButtonBackground(e);
		}

		/// <summary>
		/// Occurs when the display style changes.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderStatusStripSizingGrip(ToolStripRenderEventArgs e)
		{
			base.OnRenderStatusStripSizingGrip(e);
		}

		/// <summary>
		/// This method renders the GridStrip control's background.
		/// Occurs when the background for a ToolStrip is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
		{
			if (Skin == null)
			{
				base.OnRenderToolStripBackground(e);
				return;
			}

			e.ToolStrip.BackColor = Skin.CachedProperties.ControlColor;

			if (e.ToolStrip is SBPweb.Controls.Windows.Bar.MenuStrip)
			{
				DrawMainMenu(e);
			}
			else if (e.ToolStrip is Bar || e.ToolStrip.GetType() == typeof(ToolStrip))
			{
				DrawBar(e);
			}
			else if (e.ToolStrip is ToolStripOverflow)
			{
				e.ToolStrip.BackColor = Skin.CachedProperties.ControlColor;
			}
		}

		/// <summary>
		/// This method draws a border around the GridStrip control.
		/// Occurs when the border for a ToolStrip is rendered.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
		{
			base.OnRenderToolStripBorder(e);
		}

		/// <summary>
		/// Draws the background of a ToolStripContentPanel. 
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderToolStripContentPanelBackground(ToolStripContentPanelRenderEventArgs e)
		{
			base.OnRenderToolStripContentPanelBackground(e);
		}

		/// <summary>
		/// Draws the background of a ToolStripPanel. 
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderToolStripPanelBackground(ToolStripPanelRenderEventArgs e)
		{
			base.OnRenderToolStripPanelBackground(e);
		}

		/// <summary>
		/// Draws the background of a ToolStripStatusLabel.
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnRenderToolStripStatusLabelBackground(ToolStripItemRenderEventArgs e)
		{
			base.OnRenderToolStripStatusLabelBackground(e);
		}

		private void DrawBar(ToolStripRenderEventArgs e)
		{
			bool verticalLocation = e.ToolStrip.Orientation == Orientation.Vertical;

			e.ToolStrip.Parent.BackColor = Skin.CachedProperties.ControlColor;
			#region Bar
			sizingMargin = verticalLocation ? Skin.CachedProperties.BarVerticalMargin : Skin.CachedProperties.BarHorizontalMargin;

			System.Drawing.Image img = /*verticalLocation ? Skin.CachedProperties.BarVerticalImage :*/ Skin.CachedProperties.BarHorizontalImage;
			
			Rectangle drawBarRectangle = verticalLocation ? new Rectangle(e.ToolStrip.ClientRectangle.X, e.ToolStrip.ClientRectangle.Y + 10, e.ToolStrip.ClientRectangle.Width, e.ToolStrip.ClientRectangle.Height - 10 - ((e.ToolStrip as Bar).ShowCustomization ? 15 : 0)) : new Rectangle(e.ToolStrip.ClientRectangle.X + 10, e.ToolStrip.ClientRectangle.Y, e.ToolStrip.ClientRectangle.Width - 10 - ((e.ToolStrip as Bar).ShowCustomization ? 15 : 0), e.ToolStrip.ClientRectangle.Height);
			
			#region Window parts
			Rectangle tLT = new Rectangle(drawBarRectangle.Left, drawBarRectangle.Top, sizingMargin.Left, sizingMargin.Top);
			Rectangle tT = new Rectangle(drawBarRectangle.Left + sizingMargin.Left, drawBarRectangle.Top, drawBarRectangle.Width - sizingMargin.Horizontal, sizingMargin.Top);
			Rectangle tRT = new Rectangle(drawBarRectangle.Right - sizingMargin.Right, drawBarRectangle.Top, sizingMargin.Right, sizingMargin.Top);
			Rectangle tLM = new Rectangle(drawBarRectangle.Left, drawBarRectangle.Top + sizingMargin.Top, sizingMargin.Left, drawBarRectangle.Height - sizingMargin.Vertical);
			Rectangle tM = new Rectangle(drawBarRectangle.Left + sizingMargin.Left, drawBarRectangle.Top + sizingMargin.Top, drawBarRectangle.Width - sizingMargin.Horizontal, drawBarRectangle.Height - sizingMargin.Vertical);
			Rectangle tRM = new Rectangle(drawBarRectangle.Right - sizingMargin.Right, drawBarRectangle.Top + sizingMargin.Top, sizingMargin.Right, drawBarRectangle.Height - sizingMargin.Vertical);
			Rectangle tLB = new Rectangle(drawBarRectangle.Left, drawBarRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Left, sizingMargin.Bottom);
			Rectangle tB = new Rectangle(drawBarRectangle.Left + sizingMargin.Left, drawBarRectangle.Bottom - sizingMargin.Bottom, drawBarRectangle.Width - sizingMargin.Horizontal, sizingMargin.Bottom);
			Rectangle tRB = new Rectangle(drawBarRectangle.Right - sizingMargin.Right, drawBarRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Right, sizingMargin.Bottom);
			#endregion

			#region Drawing
			e.Graphics.DrawImage(img, tLT, /*verticalLocation ? Skin.CachedProperties.BarVerticalImageLeftTop :*/ Skin.CachedProperties.BarHorizontalImageLeftTop, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tT, /*verticalLocation ? Skin.CachedProperties.BarVerticalImageMiddleTop :*/ Skin.CachedProperties.BarHorizontalImageMiddleTop, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRT, /*verticalLocation ? Skin.CachedProperties.BarVerticalImageRightTop :*/ Skin.CachedProperties.BarHorizontalImageRightTop, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLM, /*verticalLocation ? Skin.CachedProperties.BarVerticalImageLeftMiddle :*/ Skin.CachedProperties.BarHorizontalImageLeftMiddle, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tM, /*verticalLocation ? Skin.CachedProperties.BarVerticalImageMiddle :*/ Skin.CachedProperties.BarHorizontalImageMiddle, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRM, /*verticalLocation ? Skin.CachedProperties.BarVerticalImageRightMiddle :*/ Skin.CachedProperties.BarHorizontalImageRightMiddle, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLB, /*verticalLocation ? Skin.CachedProperties.BarVerticalImageLeftBottom :*/ Skin.CachedProperties.BarHorizontalImageLeftBottom, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tB, /*verticalLocation ? Skin.CachedProperties.BarVerticalImageMiddleBottom :*/ Skin.CachedProperties.BarHorizontalImageMiddleBottom, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRB, /*verticalLocation ? Skin.CachedProperties.BarVerticalImageRightBottom :*/ Skin.CachedProperties.BarHorizontalImageRightBottom, GraphicsUnit.Pixel);
			#endregion
			#endregion

			#region BarDrag
			System.Drawing.Image imgDrag = Skin.GetImageFromBitmapResource(string.Format("{0}bars.bardrag{1}.png", Skin.KeyPrefix, verticalLocation ? "vertical" : string.Empty));
			Margin dragSizingMargin = new Margin(Skin.SelectSingleNode("Elements/BarDrag/Image/SizingMargins"));

			int imgDragWidth = imgDrag.Width;
			int imgDragHeight = imgDrag.Height;

			#region Image parts
			Rectangle idLT = new Rectangle(0, 0, dragSizingMargin.Left, dragSizingMargin.Top);
			Rectangle idT = new Rectangle(dragSizingMargin.Left, 0, imgDragWidth - dragSizingMargin.Horizontal, dragSizingMargin.Top);
			Rectangle idRT = new Rectangle(imgDragWidth - dragSizingMargin.Right, 0, dragSizingMargin.Right, dragSizingMargin.Top);
			Rectangle idLM = new Rectangle(0, dragSizingMargin.Top, dragSizingMargin.Left, (imgDragHeight - 1) - dragSizingMargin.Vertical);
			Rectangle idM = new Rectangle(dragSizingMargin.Left, dragSizingMargin.Top, imgDragWidth - dragSizingMargin.Horizontal, (imgDragHeight - 1) - dragSizingMargin.Vertical);
			Rectangle idRM = new Rectangle(imgDragWidth - dragSizingMargin.Right, dragSizingMargin.Top, dragSizingMargin.Right, (imgDragHeight - 1) - dragSizingMargin.Vertical);
			Rectangle idLB = new Rectangle(0, imgDragHeight - dragSizingMargin.Bottom, dragSizingMargin.Left, dragSizingMargin.Bottom);
			Rectangle idB = new Rectangle(dragSizingMargin.Left, imgDragHeight - dragSizingMargin.Bottom, imgDragWidth - dragSizingMargin.Horizontal, dragSizingMargin.Bottom);
			Rectangle idRB = new Rectangle(imgDragWidth - dragSizingMargin.Right, imgDragHeight - dragSizingMargin.Bottom, dragSizingMargin.Right, dragSizingMargin.Bottom);
			#endregion

			Rectangle drawDragRectangle = new Rectangle(drawBarRectangle.X - 10, drawBarRectangle.Y, 10, drawBarRectangle.Height);
			if (verticalLocation)
			{
				drawDragRectangle = new Rectangle(drawBarRectangle.X, drawBarRectangle.Y - 10, drawBarRectangle.Width, 10);
			}
			#region Window parts
			Rectangle tdLT = new Rectangle(drawDragRectangle.Left, drawDragRectangle.Top, dragSizingMargin.Left, dragSizingMargin.Top);
			Rectangle tdT = new Rectangle(drawDragRectangle.Left + dragSizingMargin.Left, drawDragRectangle.Top, drawDragRectangle.Width - dragSizingMargin.Horizontal, dragSizingMargin.Top);
			Rectangle tdRT = new Rectangle(drawDragRectangle.Right - dragSizingMargin.Right, drawDragRectangle.Top, dragSizingMargin.Right, dragSizingMargin.Top);
			Rectangle tdLM = new Rectangle(drawDragRectangle.Left, drawDragRectangle.Top + dragSizingMargin.Top, dragSizingMargin.Left, drawDragRectangle.Height - dragSizingMargin.Vertical);
			Rectangle tdM = new Rectangle(drawDragRectangle.Left + dragSizingMargin.Left, drawDragRectangle.Top + dragSizingMargin.Top, drawDragRectangle.Width - dragSizingMargin.Horizontal, drawDragRectangle.Height - dragSizingMargin.Vertical);
			Rectangle tdRM = new Rectangle(drawDragRectangle.Right - dragSizingMargin.Right, drawDragRectangle.Top + dragSizingMargin.Top, dragSizingMargin.Right, drawDragRectangle.Height - dragSizingMargin.Vertical);
			Rectangle tdLB = new Rectangle(drawDragRectangle.Left, drawDragRectangle.Bottom - dragSizingMargin.Bottom, dragSizingMargin.Left, dragSizingMargin.Bottom);
			Rectangle tdB = new Rectangle(drawDragRectangle.Left + dragSizingMargin.Left, drawDragRectangle.Bottom - dragSizingMargin.Bottom, drawDragRectangle.Width - dragSizingMargin.Horizontal, dragSizingMargin.Bottom);
			Rectangle tdRB = new Rectangle(drawDragRectangle.Right - dragSizingMargin.Right, drawDragRectangle.Bottom - dragSizingMargin.Bottom, dragSizingMargin.Right, dragSizingMargin.Bottom);
			#endregion

			#region Drawing
			e.Graphics.DrawImage(imgDrag, tdLT, idLT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgDrag, tdT, idT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgDrag, tdRT, idRT, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgDrag, tdLM, idLM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgDrag, tdM, idM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgDrag, tdRM, idRM, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgDrag, tdLB, idLB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgDrag, tdB, idB, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(imgDrag, tdRB, idRB, GraphicsUnit.Pixel);
			#endregion

			#region BarDragGlyph
			System.Drawing.Image imgDragGlyph = Skin.GetImageFromBitmapResource(string.Format("{0}bars.bardrag{1}_glyph.png", Skin.KeyPrefix, verticalLocation ? "vertical" : string.Empty));
			if (imgDragGlyph != null)
			{
				Point barDragGlyphPoint = new Point(drawDragRectangle.X + (drawDragRectangle.Width - imgDragGlyph.Width) / 2, drawDragRectangle.Y + (drawDragRectangle.Height - imgDragGlyph.Height) / 2);
				e.Graphics.DrawImage(imgDragGlyph, barDragGlyphPoint);
			}

			#endregion
			#endregion

			if ((e.ToolStrip as Bar).ShowCustomization)
			{
				#region BarCustomize
				System.Drawing.Image imgCust = Skin.GetImageFromBitmapResource(string.Format("{0}bars.barcustomize{1}.png", Skin.KeyPrefix, verticalLocation ? "vertical" : string.Empty));
				Margin custSizingMargin = new Margin(Skin.SelectSingleNode(string.Format("Elements/BarCustomize{0}/Image/SizingMargins", verticalLocation ? "Vertical" : string.Empty)));
				bool imageVertically = Skin.SelectSingleNode(string.Format("Elements/BarCustomize{0}/Image", verticalLocation ? "Vertical" : string.Empty)).Attributes["Layout"].Value.ToLower() == "vertical";

				int imgCustCount = 2;
				int imgCustWidth = imgCust.Width;
				int imgCustHeight = (Int32)(imgCust.Height / imgCustCount);
				int imgCustTop = 0;
				int imgCustLeft = 0;
				bool showDefaultCustomization = true;
				if (!imageVertically)
				{
					imgCustWidth = imgCust.Width / imgCustCount;
					imgCustHeight = imgCust.Height;
				}

				Point mousePos = Control.MousePosition;
				Point controlPos = e.ToolStrip.PointToClient(mousePos);
				if (
					(
					 (e.ToolStrip as Bar).OpenedCustomization
					)
					||
					(
					 !verticalLocation &&
					 (
					  controlPos.X > e.ToolStrip.Width - 15 && controlPos.X < e.ToolStrip.Width &&
					  controlPos.Y > 0 && controlPos.Y < e.ToolStrip.Height
					 )
					)
					||
					(
					  verticalLocation &&
					  (
					   controlPos.X > 0 && controlPos.X < e.ToolStrip.Width &&
					   controlPos.Y > e.ToolStrip.Height - 15 && controlPos.Y < e.ToolStrip.Height
					  )
					)
				   )
				{
					showDefaultCustomization = false;
				}

				if (!showDefaultCustomization)
				{
					if (!imageVertically)
					{
						imgCustLeft = imgCustWidth;
					}
					else
					{
						imgCustTop = imgCustHeight;
					}
				}

				#region Image parts
				Rectangle icLT = new Rectangle(imgCustLeft, imgCustTop, custSizingMargin.Left, custSizingMargin.Top);
				Rectangle icT = new Rectangle(imgCustLeft + custSizingMargin.Left, imgCustTop, imgCustWidth - custSizingMargin.Horizontal, custSizingMargin.Top);
				Rectangle icRT = new Rectangle(imgCustLeft + imgCustWidth - custSizingMargin.Right, imgCustTop, custSizingMargin.Right, custSizingMargin.Top);
				Rectangle icLM = new Rectangle(imgCustLeft, imgCustTop + custSizingMargin.Top, custSizingMargin.Left, imgCustHeight - custSizingMargin.Vertical);
				Rectangle icM = new Rectangle(imgCustLeft + custSizingMargin.Left, imgCustTop + custSizingMargin.Top, imgCustWidth - custSizingMargin.Horizontal, imgCustHeight - custSizingMargin.Vertical);
				Rectangle icRM = new Rectangle(imgCustLeft + imgCustWidth - custSizingMargin.Right, imgCustTop + custSizingMargin.Top, custSizingMargin.Right, imgCustHeight - custSizingMargin.Vertical);
				Rectangle icLB = new Rectangle(imgCustLeft, imgCustTop + imgCustHeight - custSizingMargin.Bottom, custSizingMargin.Left, custSizingMargin.Bottom);
				Rectangle icB = new Rectangle(imgCustLeft + custSizingMargin.Left, imgCustTop + imgCustHeight - custSizingMargin.Bottom, imgCustWidth - custSizingMargin.Horizontal, custSizingMargin.Bottom);
				Rectangle icRB = new Rectangle(imgCustLeft + imgCustWidth - custSizingMargin.Right, imgCustTop + imgCustHeight - custSizingMargin.Bottom, custSizingMargin.Right, custSizingMargin.Bottom);
				#endregion

				Rectangle drawCustRectangle = new Rectangle(e.ToolStrip.ClientRectangle.Width - 15, e.ToolStrip.ClientRectangle.Y, 15, e.ToolStrip.ClientRectangle.Height);
				if (verticalLocation)
				{
					drawCustRectangle = new Rectangle(e.ToolStrip.ClientRectangle.X, e.ToolStrip.ClientRectangle.Height - 15, e.ToolStrip.ClientRectangle.Width, 15);
				}

				#region Window parts
				Rectangle tcLT = new Rectangle(drawCustRectangle.Left, drawCustRectangle.Top, custSizingMargin.Left, custSizingMargin.Top);
				Rectangle tcT = new Rectangle(drawCustRectangle.Left + custSizingMargin.Left, drawCustRectangle.Top, drawCustRectangle.Width - custSizingMargin.Horizontal, custSizingMargin.Top);
				Rectangle tcRT = new Rectangle(drawCustRectangle.Right - custSizingMargin.Right, drawCustRectangle.Top, custSizingMargin.Right, custSizingMargin.Top);
				Rectangle tcLM = new Rectangle(drawCustRectangle.Left, drawCustRectangle.Top + custSizingMargin.Top, custSizingMargin.Left, drawCustRectangle.Height - custSizingMargin.Vertical);
				Rectangle tcM = new Rectangle(drawCustRectangle.Left + custSizingMargin.Left, drawCustRectangle.Top + custSizingMargin.Top, drawCustRectangle.Width - custSizingMargin.Horizontal, drawCustRectangle.Height - custSizingMargin.Vertical);
				Rectangle tcRM = new Rectangle(drawCustRectangle.Right - custSizingMargin.Right, drawCustRectangle.Top + custSizingMargin.Top, custSizingMargin.Right, drawCustRectangle.Height - custSizingMargin.Vertical);
				Rectangle tcLB = new Rectangle(drawCustRectangle.Left, drawCustRectangle.Bottom - custSizingMargin.Bottom, custSizingMargin.Left, custSizingMargin.Bottom);
				Rectangle tcB = new Rectangle(drawCustRectangle.Left + custSizingMargin.Left, drawCustRectangle.Bottom - custSizingMargin.Bottom, drawCustRectangle.Width - custSizingMargin.Horizontal, custSizingMargin.Bottom);
				Rectangle tcRB = new Rectangle(drawCustRectangle.Right - custSizingMargin.Right, drawCustRectangle.Bottom - custSizingMargin.Bottom, custSizingMargin.Right, custSizingMargin.Bottom);
				#endregion

				#region Drawing
				e.Graphics.DrawImage(imgCust, tcLT, icLT, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgCust, tcT, icT, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgCust, tcRT, icRT, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgCust, tcLM, icLM, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgCust, tcM, icM, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgCust, tcRM, icRM, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgCust, tcLB, icLB, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgCust, tcB, icB, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgCust, tcRB, icRB, GraphicsUnit.Pixel);
				#endregion
				#endregion

				#region BarCustomizeArrow
				System.Drawing.Image imgCustArr = Skin.GetImageFromBitmapResource(string.Format("{0}bars.barcustomize{1}_glyph.png", Skin.KeyPrefix, verticalLocation ? "vertical" : string.Empty));

				int imgCustArrCount = 2;
				int imgCustArrWidth = imgCustArr.Width;
				int imgCustArrHeight = (Int32)(imgCustArr.Height / imgCustArrCount);
				int imgCustArrTop = 0;
				int imgCustArrLeft = 0;

				if (!showDefaultCustomization)
				{
					imgCustArrTop = imgCustArrHeight;
				}

				Rectangle i = new Rectangle(imgCustArrLeft, imgCustArrTop, imgCustArrWidth, imgCustArrHeight);

				Point drawCustArrPoint = new Point(e.ToolStrip.Bounds.Width - 7 - imgCustArrWidth / 2, (drawCustRectangle.Height - imgCustArrHeight) / 2);
				if (verticalLocation)
				{
					drawCustArrPoint = new Point(drawCustRectangle.Width - 7 - imgCustArrWidth / 2, (e.ToolStrip.Bounds.Height - imgCustArrHeight) / 2);
				}

				e.Graphics.DrawImage(imgCustArr, drawCustArrPoint.X, drawCustArrPoint.Y, i, GraphicsUnit.Pixel);
				#endregion
			}
		}

		private void DrawMainMenu(ToolStripRenderEventArgs e)
		{
			sizingMargin = Skin.CachedProperties.MainMenuMargin;

			System.Drawing.Image img = Skin.CachedProperties.MainMenuImage;
			bool verticalLocation = e.ToolStrip.Orientation == Orientation.Vertical;

			Rectangle drawMenuRectangle = e.ToolStrip.ClientRectangle;
			if (e.ToolStrip.GripStyle == ToolStripGripStyle.Visible)
			{
				if (verticalLocation)
				{
					drawMenuRectangle = new Rectangle(0,  e.ToolStrip.GripRectangle.Location.Y + e.ToolStrip.GripRectangle.Height, drawMenuRectangle.Width, drawMenuRectangle.Height - e.ToolStrip.GripRectangle.Height - e.ToolStrip.GripRectangle.Location.Y);
				}
				else
				{
					drawMenuRectangle = new Rectangle(e.ToolStrip.GripRectangle.Width + e.ToolStrip.GripRectangle.Location.X - 2, 0, drawMenuRectangle.Width - e.ToolStrip.GripRectangle.Width - e.ToolStrip.GripRectangle.Location.X, drawMenuRectangle.Height);
				}
			}
			
			#region Window parts
			Rectangle tLT = new Rectangle(drawMenuRectangle.Left, drawMenuRectangle.Top, sizingMargin.Left, sizingMargin.Top);
			Rectangle tT = new Rectangle(drawMenuRectangle.Left + sizingMargin.Left, drawMenuRectangle.Top, drawMenuRectangle.Width - sizingMargin.Horizontal, sizingMargin.Top);
			Rectangle tRT = new Rectangle(drawMenuRectangle.Right - sizingMargin.Right, drawMenuRectangle.Top, sizingMargin.Right, sizingMargin.Top);
			Rectangle tLM = new Rectangle(drawMenuRectangle.Left, drawMenuRectangle.Top + sizingMargin.Top, sizingMargin.Left, drawMenuRectangle.Height - sizingMargin.Vertical);
			Rectangle tM = new Rectangle(drawMenuRectangle.Left + sizingMargin.Left, drawMenuRectangle.Top + sizingMargin.Top, drawMenuRectangle.Width - sizingMargin.Horizontal, drawMenuRectangle.Height - sizingMargin.Vertical);
			Rectangle tRM = new Rectangle(drawMenuRectangle.Right - sizingMargin.Right, drawMenuRectangle.Top + sizingMargin.Top, sizingMargin.Right, drawMenuRectangle.Height - sizingMargin.Vertical);
			Rectangle tLB = new Rectangle(drawMenuRectangle.Left, drawMenuRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Left, sizingMargin.Bottom);
			Rectangle tB = new Rectangle(drawMenuRectangle.Left + sizingMargin.Left, drawMenuRectangle.Bottom - sizingMargin.Bottom, drawMenuRectangle.Width - sizingMargin.Horizontal, sizingMargin.Bottom);
			Rectangle tRB = new Rectangle(drawMenuRectangle.Right - sizingMargin.Right, drawMenuRectangle.Bottom - sizingMargin.Bottom, sizingMargin.Right, sizingMargin.Bottom);
			#endregion

			#region Drawing
			e.Graphics.DrawImage(img, tLT, Skin.CachedProperties.MainMenuImageLeftTop, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tT, Skin.CachedProperties.MainMenuImageMiddleTop, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRT, Skin.CachedProperties.MainMenuImageRightTop, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLM, Skin.CachedProperties.MainMenuImageLeftMiddle, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tM, Skin.CachedProperties.MainMenuImageMiddle, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRM, Skin.CachedProperties.MainMenuImageRightMiddle, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tLB, Skin.CachedProperties.MainMenuImageLeftBottom, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tB, Skin.CachedProperties.MainMenuImageMiddleBottom, GraphicsUnit.Pixel);
			e.Graphics.DrawImage(img, tRB, Skin.CachedProperties.MainMenuImageRightBottom, GraphicsUnit.Pixel);
			#endregion

			if (e.ToolStrip.GripStyle == ToolStripGripStyle.Visible)
			{
				System.Drawing.Image imgDrag = Skin.GetImageFromBitmapResource(string.Format("{0}bars.bardrag{1}.png", Skin.KeyPrefix, verticalLocation ? "vertical" : string.Empty));
				Margin dragSizingMargin = new Margin(Skin.SelectSingleNode("Elements/BarDrag/Image/SizingMargins"));

				int imgDragWidth = imgDrag.Width;
				int imgDragHeight = imgDrag.Height;

				#region Image parts
				Rectangle idLT = new Rectangle(0, 0, dragSizingMargin.Left, dragSizingMargin.Top);
				Rectangle idT = new Rectangle(dragSizingMargin.Left, 0, imgDragWidth - dragSizingMargin.Horizontal, dragSizingMargin.Top);
				Rectangle idRT = new Rectangle(imgDragWidth - dragSizingMargin.Right, 0, dragSizingMargin.Right, dragSizingMargin.Top);
				Rectangle idLM = new Rectangle(0, dragSizingMargin.Top, dragSizingMargin.Left, (imgDragHeight - 1) - dragSizingMargin.Vertical);
				Rectangle idM = new Rectangle(dragSizingMargin.Left, dragSizingMargin.Top, imgDragWidth - dragSizingMargin.Horizontal, (imgDragHeight - 1) - dragSizingMargin.Vertical);
				Rectangle idRM = new Rectangle(imgDragWidth - dragSizingMargin.Right, dragSizingMargin.Top, dragSizingMargin.Right, (imgDragHeight - 1) - dragSizingMargin.Vertical);
				Rectangle idLB = new Rectangle(0, imgDragHeight - dragSizingMargin.Bottom, dragSizingMargin.Left, dragSizingMargin.Bottom);
				Rectangle idB = new Rectangle(dragSizingMargin.Left, imgDragHeight - dragSizingMargin.Bottom, imgDragWidth - dragSizingMargin.Horizontal, dragSizingMargin.Bottom);
				Rectangle idRB = new Rectangle(imgDragWidth - dragSizingMargin.Right, imgDragHeight - dragSizingMargin.Bottom, dragSizingMargin.Right, dragSizingMargin.Bottom);
				#endregion

				Rectangle drawDragRectangle = (e.ToolStrip as MenuStrip).GripRectangle;
				if (verticalLocation)
				{
					drawDragRectangle = new Rectangle(0, 0, drawMenuRectangle.Width, drawDragRectangle.Height + drawDragRectangle.Location.Y);
				}
				else
				{
					drawDragRectangle = new Rectangle(0, 0, drawDragRectangle.Width + drawDragRectangle.Location.X - 2, drawMenuRectangle.Height);
				}

				#region Window parts
				Rectangle tdLT = new Rectangle(drawDragRectangle.Left, drawDragRectangle.Top, dragSizingMargin.Left, dragSizingMargin.Top);
				Rectangle tdT = new Rectangle(drawDragRectangle.Left + dragSizingMargin.Left, drawDragRectangle.Top, drawDragRectangle.Width - dragSizingMargin.Horizontal, dragSizingMargin.Top);
				Rectangle tdRT = new Rectangle(drawDragRectangle.Right - dragSizingMargin.Right, drawDragRectangle.Top, dragSizingMargin.Right, dragSizingMargin.Top);
				Rectangle tdLM = new Rectangle(drawDragRectangle.Left, drawDragRectangle.Top + dragSizingMargin.Top, dragSizingMargin.Left, drawDragRectangle.Height - dragSizingMargin.Vertical);
				Rectangle tdM = new Rectangle(drawDragRectangle.Left + dragSizingMargin.Left, drawDragRectangle.Top + dragSizingMargin.Top, drawDragRectangle.Width - dragSizingMargin.Horizontal, drawDragRectangle.Height - dragSizingMargin.Vertical);
				Rectangle tdRM = new Rectangle(drawDragRectangle.Right - dragSizingMargin.Right, drawDragRectangle.Top + dragSizingMargin.Top, dragSizingMargin.Right, drawDragRectangle.Height - dragSizingMargin.Vertical);
				Rectangle tdLB = new Rectangle(drawDragRectangle.Left, drawDragRectangle.Bottom - dragSizingMargin.Bottom, dragSizingMargin.Left, dragSizingMargin.Bottom);
				Rectangle tdB = new Rectangle(drawDragRectangle.Left + dragSizingMargin.Left, drawDragRectangle.Bottom - dragSizingMargin.Bottom, drawDragRectangle.Width - dragSizingMargin.Horizontal, dragSizingMargin.Bottom);
				Rectangle tdRB = new Rectangle(drawDragRectangle.Right - dragSizingMargin.Right, drawDragRectangle.Bottom - dragSizingMargin.Bottom, dragSizingMargin.Right, dragSizingMargin.Bottom);
				#endregion

				#region Drawing
				e.Graphics.DrawImage(imgDrag, tdLT, idLT, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgDrag, tdT, idT, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgDrag, tdRT, idRT, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgDrag, tdLM, idLM, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgDrag, tdM, idM, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgDrag, tdRM, idRM, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgDrag, tdLB, idLB, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgDrag, tdB, idB, GraphicsUnit.Pixel);
				e.Graphics.DrawImage(imgDrag, tdRB, idRB, GraphicsUnit.Pixel);
				#endregion
			}
		}

		#endregion

		#region ISkinnable Members

		/// <summary>
		/// Use default skin or not?
		/// </summary>
		[Category("Look && feel")]
		public bool UseDefaultSkin
		{
			get
			{
				return mUseDefaultSkin;
			}
			set
			{
				mUseDefaultSkin = value;
			}
		}

		/// <summary>
		/// Used skin
		/// </summary>
		[Category("Look && feel")]
		public Skin Skin
		{
			get
			{
				if (this.UseDefaultSkin)
				{
					return DefaultLookAndFeel.Skin;
				}

				return mSkin;
			}
			set
			{
				mSkin = value;
			}
		}

		#endregion

	}
}
